package com.mengma.ec.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.entity.Shipping;
import com.mengma.ec.entity.User;
import com.mengma.ec.service.portal.IShippingService;
import com.mengma.ec.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Auther: xixi
 * @Date: 2018/9/25 09:27
 * @Description:
 */
@RequestMapping("/shipping/")
@RestController
public class ShippingController {

    @Autowired
    private IShippingService iShippingService;

    @PostMapping("add.do")
    public ServerResponse<String> add(@RequestBody Shipping shipping, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iShippingService.add(user.getId(), shipping);
        return response;
    }

    @PostMapping("delete.do")
    public ServerResponse<String> delete(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iShippingService.delete(shippingId);
        return response;
    }

    @PostMapping("select.do")
    public ServerResponse<String> select(HttpSession session, Integer shippingId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iShippingService.select(shippingId);
        return response;
    }

    @GetMapping("list.do")
    public ServerResponse<PageInfo> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize,
                                         HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iShippingService.findListData(pageNum, pageSize, user.getId());
        return response;

    }

    @PostMapping("updata.do")
    public ServerResponse<String> updata(HttpSession session, @RequestBody Shipping shipping) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {
            return ServerResponse.createByErrorMessage("脑子是个好东西！请先登录！");
        }
        ServerResponse response = iShippingService.updata(user.getId(), shipping);
        return response;
    }
}
